package pl.shenlon.poker.cards;

import java.util.Collections;
import java.util.Observable;

public class Hand extends Observable {

	public static final Hand EMPTY_HAND = new Hand(0);

	private final Card[] cards;

	public Hand(Card... cards) {
		this.cards = cards;
	}

	public Hand(int size) {
		cards = new Card[size];
	}

	public Hand(String handString) {
		String[] cards = handString.split("-");
		this.cards = new Card[cards.length];
		for (int i = 0; i < cards.length; i++) {
			this.cards[i] = new Card(cards[i]);
		}
	}

	public boolean containsCard(Card card) {
		for (Card cardFromHand : cards) {
			if (cardFromHand.equals(card) && cardFromHand.isFaceUp()) {
				return true;
			}
		}
		return false;
	}

	public boolean containsCard(CardSuit suit, CardRank rank) {
		for (Card cardFromHand : cards) {
			if (cardFromHand.equals(suit, rank) && cardFromHand.isFaceUp()) {
				return true;
			}
		}
		return false;
	}

	public boolean containsCardIgnoreFaceUp(Card card) {
		for (Card cardFromHand : cards) {
			if (cardFromHand.equals(card)) {
				return true;
			}
		}
		return false;
	}

	public boolean containsRank(CardRank rank) {
		for (Card card : cards) {
			if (card.getRank().equals(rank)) {
				return true;
			}
		}
		return false;
	}

	public Card getCard(int position) {
		return cards[position];
	}

	public Card[] getCards() {
		return cards;
	}

	public int getCardsCount() {

		return cards.length;
	}

	public int getRanksValue() {
		int sum = 0;
		for (Card card : cards) {
			sum += card.getRank().ordinal();
		}
		return sum;
	}
	public int getVisibleCardsCount() {
		int count = 0;
		for (Card card : cards) {
			if (card.isFaceUp()) {
				count++;
			}
		}
		return count;
	}

	public void setCard(int position, Card card) {
		cards[position] = card;
		setChanged();
		notifyObservers(String.format("setCard(%d, %s)", position, card));
	}

	public void setCards(Card... cards) {
		for (int i = 0; i < cards.length; i++) {
			this.cards[i] = cards[i];
		}
		setChanged();
		notifyObservers(String.format("setCards(%d)", cards.length));
	}

	public void show() {
		for (Card card : cards) {
			card.setFaceUp(true);
		}
		setChanged();
		notifyObservers("show()");
	}

	public void show(int... indexes) {
		for (int index : indexes) {
			cards[index].setFaceUp(true);
		}
		setChanged();
		notifyObservers(String.format("show(%d)", indexes.length));
	}

	public void sortAscending() {
		java.util.Arrays.sort(cards);
		setChanged();
		notifyObservers("sortAscending()");
	}

	public void sortDescending() {
		java.util.Arrays.sort(cards, Collections.reverseOrder());
		setChanged();
		notifyObservers("sortDescending()");
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (Card card : cards) {
			sb.append(card).append("-");
		}
		sb.setLength(sb.length() - 1);
		return sb.toString();
	}
}